package org.goldenport.g3.servers

import scala.actors._
import org.goldenport.g3._

/**
 * @since   Nov. 29, 2010
 * @version Mar. 27, 2011
 * @author  ASAMI, Tomoharu
 */
class TimerServer(val context: G3Context, anApp: Option[G3Application]) extends Actor {
  def this(c: G3Context) = this(c, None)

  private lazy val _g3app = {
    anApp match {
      case Some(a) => a
      case None => {
	val appName = context.getParameterString("g3.application",
						 "App")
	val a = Class.forName(appName).newInstance().asInstanceOf[G3Application]
	a.openTimer()
	a
      }
    }
  }

  def act() {
    loop {
      Thread.sleep(5000)
      // TODO get timer schedule from app and optimize timer clock
//      println("before timer")
      _g3app.timer()
//      println("after timer")
    }
  }
}

object TimerServer {
  def start(context: G3Context, app: Option[G3Application]) {
//    println("timer")
    val timer = new TimerServer(context, app)
//    println("timer = " + timer)
    timer.start
  }
}
